using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace MES
{
    public partial class frmCorrect : Form
    {
        private static frmCorrect instance = null;
        private frmCorrect()
        {
            InitializeComponent();
        }
        public static frmCorrect createInstance()
        {
            if (instance == null)
            {
                instance = new frmCorrect();
            }
            return instance;
        }

        private void Clear_Defect_Code()
        {
            if (this.tbDefectPack.Focused)
            {
                using (SqlConnection conn = getConnection())
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "CMS.M2.Clear_Defect_Code";
                    cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = this.tbDefectPack.Text;
                    cmd.Parameters.Add("@TYPE", SqlDbType.Char, 8).Value = this.rbDefault.Checked ? "Default" : "Next";
                    SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 64);
                    res.Direction = ParameterDirection.Output;
                    cmd.Connection = conn;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    if (res.Value.ToString().Contains("OK"))
                    {
                        this.lbRecords.Items.Add(this.tbDefectPack.Text.ToString() + " " + res.Value.ToString());
                    }
                    else
                    {
                        this.lbRecords.Items.Add(this.tbDefectPack.Text.ToString() + " " + res.Value.ToString());
                    }
                    this.tbDefectPack.Clear();
                    this.tbDefectPack.Focus();
                }
            }
        }

        private bool validatePack(string pack_no)
        {
            bool res = true;
            if (pack_no.Length != 9)
            {
                return false;
            }
            pack_no = pack_no.ToUpper();
            char[] str = pack_no.ToCharArray();
            foreach (char var in str)
            {
                if ((var >= 48 && var <= 57) || (var >= 65 && var <= 97))
                {
                    continue;
                }
                else
                {
                    res = false;
                    break;
                }
            }
            if (res == false)
            {
                return false;
            }
            return res;
        }
        private bool validateCustomerSerialNo(string customer)
        {
            bool res = true;
            if (customer.Length != 13 && customer.Length != 14)
            {
                return false;
            }
            customer = customer.ToUpper();
            char[] str = customer.ToCharArray();
            foreach (char var in str)
            {
                if ((var >= 48 && var <= 57) || (var >= 65 && var <= 97))
                {
                    continue;
                }
                else
                {
                    res = false;
                    break;
                }
            }
            return res;
        }
        private SqlConnection getConnection()
        {
            SqlConnection conn = null;
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            try
            {
                conn = new SqlConnection(config.ConnectionStrings.ConnectionStrings["MES.Properties.Settings.CMS"].ToString());
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Get connection error!", MessageBoxButtons.OK);
            }
            return conn;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void frmCorrect_Load(object sender, EventArgs e)
        {
            this.rbOthers.Checked = true;
            this.cbNew.SelectedIndex = 0;
            this.cbOld.SelectedIndex = 0;
        }
        private void change_Mold(string pack_no, string oldOne, string newOne)
        {
            if (oldOne == newOne)
            {
                MessageBox.Show("the old mold no equla to the new one!", "Mold no Change", MessageBoxButtons.OK);
                return;
            }
            using (SqlConnection conn = getConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "CMS.M2.Change_Mold";
                cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = tbMoldPack.Text.Trim().ToUpper();
                cmd.Parameters.Add("@Old", SqlDbType.Char, 4).Value = cbOld.Text.Trim().ToUpper();
                cmd.Parameters.Add("@New", SqlDbType.Char, 4).Value = cbNew.Text.Trim().ToUpper();
                SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 64);
                res.Direction = ParameterDirection.Output;
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                if (res.Value.ToString().Contains("OK"))
                {
                    this.lbMold.Items.Add(this.tbMoldPack.Text.ToString() + " " + res.Value.ToString());
                }
                else
                {
                    this.lbMold.Items.Add(this.tbMoldPack.Text.ToString() + " " + res.Value.ToString());
                }
                this.tbMoldPack.Clear();
                this.tbMoldPack.Focus();
            }
        }
        private void disable_Pack(string pack_no)
        {
            using (SqlConnection conn = getConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "CMS.M2.Disable_Pack";
                cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = pack_no.Trim().ToUpper();
                SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 64);
                res.Direction = ParameterDirection.Output;
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                if (res.Value.ToString().Contains("OK"))
                {
                    MessageBox.Show(this.tbCellPack.Text.ToString() + " " + res.Value.ToString());
                }
                else
                {
                    MessageBox.Show(this.tbCellPack.Text.ToString() + " updated failed!");
                }
                this.tbCellPack.Clear();
                this.tbCellPack.Focus();
            }

        }
        private void change_Line(string pack_no)
        {
            using (SqlConnection conn = getConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "CMS.M2.Change_Line";
                cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = pack_no.Trim().ToUpper();
                cmd.Parameters.Add("@New", SqlDbType.Char, 4).Value = cbLines.Text.Trim().ToUpper();
                SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 64);
                res.Direction = ParameterDirection.Output;
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                if (res.Value.ToString().Contains("OK"))
                {
                    MessageBox.Show(pack_no + " " + res.Value.ToString());
                }
                else
                {
                    MessageBox.Show(pack_no + " line updated failed!");
                }
                this.tbLinePack.Clear();
                this.tbLinePack.Focus();
            }

        }
        private void check_Pack(string pack_no)
        {
            using (SqlConnection conn = getConnection())
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "CMS.M2.Check_Pack";
                cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = pack_no.Trim().ToUpper();
                SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 64);
                res.Direction = ParameterDirection.Output;
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
                if (res.Value.ToString().Contains("OK"))
                {
                    MessageBox.Show(res.Value.ToString());
                }
                else
                {
                    MessageBox.Show(res.Value.ToString());
                }              
            }

        }
        private void tbPackNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (validatePack(((TextBox)sender).Text.Trim()))
                {
                    switch (((TextBox)sender).Name.ToString())
                    {
                        case "tbDefectPack":
                            Clear_Defect_Code();
                            break;
                        case "tbMoldPack":
                            change_Mold(((TextBox)sender).Text, cbOld.Text, cbNew.Text);
                            break;
                        case "tbCellPack":
                            disable_Pack(((TextBox)sender).Text.Trim().ToUpper());
                            break;
                        case "tbLinePack":
                            change_Line(((TextBox)sender).Text.Trim().ToUpper());
                            break;
                        case "tbPackCustomer":
                            check_Pack(tbPackCustomer.Text.Trim().ToUpper());
                            break;
                        default:
                            break;
                    }
                    ((TextBox)sender).SelectAll();
                    btnLink.Focus();
                }
            }
        }
        private void tpPackCustSN_Click(object sender, EventArgs e)
        {
            this.tbCustomerSerial.Focus();
        }
        private void tbCustomerSerial_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (validateCustomerSerialNo(((TextBox)sender).Text.Trim()))
                {
                    using (SqlConnection conn = getConnection())
                    {
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "CMS.M2.Check_SerialNo";
                        cmd.Parameters.Add("@CUST_SNO", SqlDbType.Char, 16).Value = tbCustomerSerial.Text.Trim().ToUpper();
                        SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 128);
                        res.Direction = ParameterDirection.Output;
                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();
                            if (res.Value.ToString().Contains("OK"))
                            {
                                this.lbCustomer.Items.Add(this.tbCustomerSerial.Text.ToString() + " " + res.Value.ToString());
                            }
                            else
                            {
                                this.lbCustomer.Items.Add(this.tbCustomerSerial.Text.ToString() + " " + res.Value.ToString());
                            }

                        }
                        catch (Exception exe)
                        {
                            conn.Close();
                            MessageBox.Show(exe.Message);
                        }
                        MessageBox.Show("Execute result " + res.Value, "Customer Serial Number Analyse", MessageBoxButtons.OK);
                        tbCustomerSerial.SelectAll();
                        tbPackCustomer.Focus();                        

                    }
                }
            }

        }
        private void btnLink_Click(object sender, EventArgs e)
        {
            if (tbCustomerSerial.Text.Trim()!=string.Empty && tbPackCustomer.Text.Trim() != string.Empty)
            {
                if (validateCustomerSerialNo(tbCustomerSerial.Text.Trim().ToUpper()))
                {
                    if (validatePack(tbPackCustomer.Text.Trim().ToUpper()))
                    {
                        using (SqlConnection conn = getConnection())
                        {
                            SqlCommand cmd = new SqlCommand();
                            cmd.Connection = conn;
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "CMS.M2.Link_Pack_SerialNo";
                            cmd.Parameters.Add("@CUST_SNO", SqlDbType.Char, 16).Value = tbCustomerSerial.Text.Trim().ToUpper();
                            cmd.Parameters.Add("@PACK_NO", SqlDbType.Char, 16).Value = this.tbPackCustomer.Text.Trim().ToUpper();
                            SqlParameter res = cmd.Parameters.Add("@RES", SqlDbType.Char, 128);
                            res.Direction = ParameterDirection.Output;
                            try
                            {
                                conn.Open();
                                cmd.ExecuteNonQuery();
                                if (res.Value.ToString().Contains("OK"))
                                {
                                    this.lbCustomer.Items.Add(this.tbCustomerSerial.Text.ToString() + " " + res.Value.ToString());
                                }
                                else
                                {
                                    this.lbCustomer.Items.Add(this.tbCustomerSerial.Text.ToString() + " " + res.Value.ToString());
                                }

                            }
                            catch (Exception exe)
                            {
                                conn.Close();
                                MessageBox.Show(exe.Message);
                            }
                            MessageBox.Show("Execute result " + res.Value, "Customer Serial Number Analyse", MessageBoxButtons.OK);
                            tbCustomerSerial.Clear();
                            tbPackCustomer.Clear();
                            tbCustomerSerial.Focus();

                        }
                    }
                }
            }
            else
	        {
                MessageBox.Show("Pack no or Customer is empty");
	        }

        }      
    }
}